package com.kgc.scd.controller;


import com.kgc.scd.bean.*;
import com.kgc.scd.service.CollectionService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created On : 2022/6/14.
 * <p>
 * Author     : 15950526365
 * <p>
 * Description: 收藏操作入口
 */


@Slf4j
@RestController
public class CollectionController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private CollectionService collectionService;

    /*
     * @author : 15950526365
     * @date   : 2022/6/15
     * @param  : [java.lang.String, java.lang.String]
     * @return : com.kgc.scd.util.RequestResult<java.util.Map<java.lang.String,java.lang.Object>>
     * @description : 收藏的添加功能
     */

    @PostMapping("/collections")
    public RequestResult<Map<String, Object>> collections(@RequestParam("userId") String userId, @RequestParam("proId") String proId) {
        Map<String, Object> returnMap = new HashMap<>();
        if (!redisUtils.checkFreq(userId + "-" + proId, 4, 5)) {
            returnMap.put("collectionMsg", "请勿频繁操作");
            return ResultBuildUtil.fail("402", "频繁操作");
        }
        // 判断当前用户是否已经收藏过当前商品，如果已经收藏，就取消
        if (redisUtils.lGet("proCollection" + ":" + userId, 0, -1).contains(proId)) {
            // 当前用户已经收藏过当前商品，直接删除收藏记录即可
            redisUtils.lRemove("proCollection" + ":" + userId, 1, proId);
            collectionService.delCollection(userId, proId);
            log.info("------ 用户编号：{}，商品编号：{}，取消收藏成功 ------", userId, proId);
            returnMap.put("collectMsg", "取消收藏成功");
            return ResultBuildUtil.success(returnMap);
        }
        //TODO 收藏商品数量已达上线,请删除商品
        if (redisUtils.lGetListSize("proCollection" + ":" + userId) >= 5) {
            return ResultBuildUtil.fail("402", "收藏商品数量已达上限，请删除商品");
        }
        redisUtils.lSet("proCollection" + ":" + userId, proId);
        Collect collect = new Collect();
        collect.setCollUid(userId);
        collect.setCollProid(proId);
        collectionService.addCollection(collect);
        returnMap.put("collectionMsg", "收藏成功");
        return ResultBuildUtil.success(returnMap);
    }

    /**
     * @param : []
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Cart>>
     * @author : 98730
     * @date : 2022/6/10
     * @description : 显示收藏列表，每页3条数据
     */

    @PostMapping("/queryCart")
    public RequestResult<PageSupport<CollectProducts>> queryCartListByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                                                                           @RequestParam(defaultValue = "3") Integer pageSize,
                                                                           @RequestParam("userId") String userId,
                                                                           @RequestParam("orderBy") Integer orderBy) {
        return ResultBuildUtil.success(collectionService.queryCartListByPage(pageNo, pageSize, userId,orderBy));
    }


    /*
     * @author : 15950526365
     * @date   : 2022/6/15
     * @param  : [java.lang.String, java.util.List<java.lang.String>]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 批量删除商品
     */

    @PostMapping("/delCollectList")
    public RequestResult<String> delCollectList(@RequestParam String userId, @RequestParam List<String> proIds) {
        for (String proId : proIds) {
            redisUtils.lRemove("proCollection" + ":" + userId, 1, proId);
        }
        collectionService.delCollectList(userId, proIds);
        return ResultBuildUtil.success("批量删除成功");
    }


    /*
     * @author : 15950526365
     * @date   : 2022/6/15
     * @param  : [java.lang.Integer, java.lang.Integer]
     * @return : com.kgc.scd.util.RequestResult<java.util.Map<java.lang.String,java.lang.Object>>
     * @description : 足迹的添加与查看 对redis 操作大于五条数据进行删除
     */

    @PostMapping("/requestFoot")
    public RequestResult<List<Foots>> requestFoot(@RequestParam String userId, @RequestParam String proId) {

        List<Object> returnList = new ArrayList<>();
        if (redisUtils.lGet("zuji" + ":" + userId, 0, -1).contains(proId)) {
            // 当前用户已经收藏过当前商品，直接删除即可
            log.info("---------发现足迹 删除足迹:{}", proId);
            redisUtils.lRemove("zuji" + ":" + userId, 1, proId);
            // TODO 根据用户id和商品id，更新数据库时间
            log.info("------ 用户编号：{}，商品编号：{}，删除足迹 ------", userId, proId);
            collectionService.deleteProId(userId, proId);
        }
        if (redisUtils.lGetListSize("zuji" + ":" + userId) >= 30) {
            redisUtils.lRemove("zuji" + ":" + userId, 1, redisUtils.lGetIndex("zuji" + ":" + userId, 0));
            collectionService.deleteProId(userId, proId);
        }
        redisUtils.lSet("zuji" + ":" + userId, proId);
        Foot foot = new Foot();
        foot.setFootUserid(userId);
        foot.setFootProid(proId);
        foot.setFootTime(new Date());
        collectionService.addProId(foot);
        returnList = redisUtils.lGet("zuji" + ":" + userId, 0, -1);


        List<Foots> footies = new ArrayList<>();
        returnList.forEach(no -> footies.add(collectionService.queryProdByFood((String)no)));
        Collections.reverse(footies);
        return ResultBuildUtil.success(footies);
    }



    /*
     * @author : 15950526365
     * @date   : 2022/6/15
     * @param  :
     * @return :
     * @description : 足迹功能显示
     */

    @PostMapping("/selectFoots")
    public RequestResult<PageSupport<Foots>> selectFoots(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "3") Integer pageSize, @RequestParam String userId) {
        return ResultBuildUtil.success(collectionService.selectFoots(pageNo, pageSize, userId));
    }

    /*
     * @author : 15950526365
     * @date   : 2022/6/16
     * @param  :
     * @return :
     * @description :  足迹满足5条开始进行删除足迹功能
     */
    @PostMapping("/delectFoots")
    public void delectFoots(@RequestParam String userId, @RequestParam String prodId) {
        collectionService.deleteProId(userId, prodId);
    }





    /*
     * @author : 15950526365
     * @date   : 2022/6/20
     * @param  :
     * @return :
     * @description : 清除足迹
     */
    @PostMapping("/clearFoot")
    public RequestResult<Object> clearFoot(@RequestParam String userId){
        redisUtils.del("zuji:"+userId);
        log.info("------------足迹功能清除----------");
        collectionService.clearFoot(userId);
        return ResultBuildUtil.success("足迹清除成功");
    }


    /*
     * @author : 15950526365
     * @date   : 2022/6/20
     * @param  :
     * @return :
     * @description :  清除收藏
     */
    @PostMapping("/clearCollection")
    public  RequestResult<Object> clearCollection(@RequestParam String userId){
        redisUtils.del("proCollection:"+userId);
        log.info("------------收藏功能清除----------");
        collectionService.clearCollection(userId);
        return  ResultBuildUtil.success("收藏清除成功");
    }




}
